<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域编号" prop="areacode">
        <el-input
          v-model="queryParams.areacode"
          placeholder="请输入区域编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['nongchanping:crops:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['nongchanping:crops:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['nongchanping:crops:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['nongchanping:crops:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cropsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品名称" align="center" prop="name" />
      <el-table-column label="区域编号" align="center" prop="areacode" />
      <el-table-column label="亩数" align="center" prop="acres" />
      <!-- <el-table-column label="种植期" align="center" prop="plantingdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.plantingdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="出芽期" align="center" prop="sprouted" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sprouted, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="成苗期" align="center" prop="matureseed" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.matureseed, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="花期" align="center" prop="flowering" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.flowering, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结果期" align="center" prop="bearfruit" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.bearfruit, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="采摘期" align="center" prop="processdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.processdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保鲜期" align="center" prop="freshnessperiod" /> -->
      <el-table-column label="图片" align="center" prop="imageurl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.imageurl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['nongchanping:crops:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['nongchanping:crops:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row,scope.index)"
          >详细</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="详细信息" :visible.sync="openView" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
 
        <el-col :span="12">
            <el-form-item label="种植期：">{{ form.plantingdate }}</el-form-item>
            <el-form-item label="出芽期："
            >{{ form.sprouted }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="成苗期：">{{ form.bearfruit }}</el-form-item>
             <el-form-item label="开花期：">{{ form.flowering }}</el-form-item>
          </el-col>  
              <el-col :span="12">
            <el-form-item label="结果期：">{{ form.matureseed }}</el-form-item>
             <el-form-item label="采摘期：">{{ form.processdate }}</el-form-item>
          </el-col>   
          <el-col :span="12">
            <el-form-item label="保鲜期：">{{ form.freshnessperiod }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openView   = false">关 闭</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改农作物对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="区域编号" prop="areacode">
          <el-input v-model="form.areacode" placeholder="请输入区域编号" />
        </el-form-item>
        <el-form-item label="亩数" prop="acres">
          <el-input v-model="form.acres" placeholder="请输入亩数" />
        </el-form-item>
        <el-form-item label="种植期" prop="plantingdate">
          <el-date-picker clearable
            v-model="form.plantingdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择种植期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="出芽期" prop="sprouted">
          <el-date-picker clearable
            v-model="form.sprouted"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出芽期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="成苗期" prop="matureseed">
          <el-date-picker clearable
            v-model="form.matureseed"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择成苗期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="花期" prop="flowering">
          <el-date-picker clearable
            v-model="form.flowering"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择花期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结果期" prop="bearfruit">
          <el-date-picker clearable
            v-model="form.bearfruit"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择结果期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="采摘期" prop="processdate">
          <el-date-picker clearable
            v-model="form.processdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择采摘期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="保鲜期" prop="freshnessperiod">
          <el-input v-model="form.freshnessperiod" placeholder="请输入保鲜期" />
        </el-form-item>
        <el-form-item label="图片">
          <image-upload v-model="form.imageurl"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCrops, getCrops, delCrops, addCrops, updateCrops } from "@/api/nongchanping/crops";

export default {
  name: "Crops",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 农作物表格数据
      cropsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openView:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        areacode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],
        areacode: [
          { required: true, message: "区域编号不能为空", trigger: "blur" }
        ],
      
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
       handleView(row) {
            console.log(row,55555)
      this.openView = true;
      this.form = row;
   
    },
    /** 查询农作物列表 */
    getList() {
      this.loading = true;
      listCrops(this.queryParams).then(response => {
        this.cropsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pid: null,
        name: null,
        areacode: null,
        acres: null,
        plantingdate: null,
        sprouted: null,
        matureseed: null,
        flowering: null,
        bearfruit: null,
        processdate: null,
        freshnessperiod: null,
        imageurl: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加农作物";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const pid = row.pid || this.ids
      getCrops(pid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改农作物";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pid != null) {
            updateCrops(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCrops(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pids = row.pid || this.ids;
      this.$modal.confirm('是否确认删除农作物编号为"' + pids + '"的数据项？').then(function() {
        return delCrops(pids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('nongchanping/crops/export', {
        ...this.queryParams
      }, `crops_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
